import {Component} from '@angular/core';
import {IonicPage, NavController} from 'ionic-angular';
import {KitProvider} from "../../../core/foundations/KitProvider";


/**
 * Generated class for the ChangePasswordPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-change-password',
  templateUrl: 'change-password.html',
})
export class EmployeeChangePasswordPage {
  oldPassword: string;
  newPassword: string;
  newPasswordConfirmation: string;

  constructor(public nav: NavController,
              public kit: KitProvider) {
  }

  changePassword() {
    if (this.oldPassword == null || this.oldPassword == '' ||
      this.newPassword == null || this.newPassword == '') {
      this.kit.alertMsg('Error', 'Please Enter Your Password!');
      return;
    }

    if (this.newPassword != this.newPasswordConfirmation) {
      this.kit.alertMsg('Error', 'The two password input is inconsistent!');
      return;
    }

    this.kit.auth.changePassword(this.oldPassword, this.newPassword).subscribe(
      response => {
        if (response.success)
          this.kit.alertMsg('Success', 'Password is Modified!');
        else
          this.kit.alertMsg('Error', 'Something of errors have occurred,Password isn\'t Modified!');
        this.nav.pop();
      }
    );
  }


}
